#include "lvgl.h"
#include "img_def.h"
#include "lv_common_function.h"

enum
{
    PAGE_ID_HOME,
    PAGE_ID_PANEL,
    PAGE_ID_CTRL,
    PAGE_ID_MUSIC,
    PAGE_ID_CAN,
	PAGE_ID_TEST,
	PAGE_ID_BT_PAN,
	PAGE_ID_MAX,
};

#define LIST_SETTING_NUM        (PAGE_ID_MAX)
 
 
static uint8_t last_select_id=0;
static int8_t cur_select_id=0;
extern volatile uint8_t lock_timer_flag;

//===============================================================
static void system_setting_event_cb(lv_event_t* e)
{
    lv_obj_t *obj = lv_event_get_target(e);
    uint8_t id = lv_obj_get_child_id(obj);
	lv_obj_t *new_child_obj;
	lv_obj_t *cur_child_obj;
	lv_obj_t *subobj  = lv_event_get_user_data(e);
	if(!lv_obj_is_valid(subobj))
		return;

	if(lv_event_get_code(e)==LV_EVENT_FOCUSED)
    {
        // printf("LV_EVENT_FOCUSED %d\n",id);
        #if 1
         if(last_select_id!=id)
         {
            new_child_obj = lv_obj_get_child(subobj,last_select_id);
            lv_obj_set_size(new_child_obj,LV_PCT(90),48);
            lv_obj_set_style_bg_color(new_child_obj,lv_color_black(),0);
            lv_obj_set_style_border_color(new_child_obj,lv_color_black(),0);

            lv_obj_set_style_text_color(lv_obj_get_child(new_child_obj,1),lv_color_make(18, 150, 219),0);
            lv_img_set_src(lv_obj_get_child(new_child_obj,0),IMG_DOT_BLUE_32);
            //lv_obj_set_x(new_child_obj,lv_obj_get_x(new_child_obj)-20);
         }
         cur_child_obj  = lv_obj_get_child(subobj,id);
         lv_obj_set_style_bg_color(cur_child_obj,lv_color_make(96, 184, 192),0);
         lv_obj_set_style_border_color(cur_child_obj,lv_color_make(96, 184, 192),0);
        // lv_obj_set_x(cur_child_obj,lv_obj_get_x(cur_child_obj)+20);
	     lv_obj_clear_state(cur_child_obj,LV_EVENT_FOCUSED);
         lv_obj_set_size(cur_child_obj,LV_PCT(90),54);
         lv_obj_set_style_text_color(lv_obj_get_child(cur_child_obj,1),lv_color_make(255, 255, 255),0);
         lv_img_set_src(lv_obj_get_child(cur_child_obj,0),IMG_DOT_WHITE_48);

	     #endif
    }else if(lv_event_get_code(e)==LV_EVENT_CLICKED)
    {
        printf("LV_EVENT_CLICKED %d\n",id);
        switch (id)
        {
        case PAGE_ID_HOME:
			 user_jump_page(fr_lv_86box_page1_func);
			 lock_timer_flag = 0;
            break;

        case PAGE_ID_PANEL:
			 user_jump_page(fr_lv_instrument_panel_km_func);
			 lock_timer_flag = 0;
            break;

        case PAGE_ID_CTRL:
			 user_jump_page(fr_lv_86box_page2_func);
			 lock_timer_flag = 0;
            break;

        case PAGE_ID_MUSIC:
			 user_jump_page(fr_lv_app_music_control_func);	
			 lock_timer_flag = 0;
            break;

        case PAGE_ID_CAN:
			 user_jump_page(fr_can_page);		
			 lock_timer_flag = 0;
            break;
		case PAGE_ID_TEST:
			user_jump_page(fr_test_page_func);	
			lock_timer_flag = 0;	
			break;
		case PAGE_ID_BT_PAN:
			user_jump_page(fr_bt_pan_page);
			lock_timer_flag = 0;
        default:
		 
            break;
        }
    }
    last_select_id=id;
}


static void event_handler(lv_event_t *e)
{
	lv_event_code_t event = lv_event_get_code(e);
	lv_obj_t *obj = lv_obj_get_child(lv_event_get_target(e),0);

	if(!lv_obj_is_valid(obj))
        return;
	if(event == LV_EVENT_DELETE)
	{

	}else if(event == LV_EVENT_KEY)
	{
		uint32_t *key = lv_event_get_param(e);
        switch(*key)
        {
        case KEY1_CODE:
             if(--cur_select_id<0)cur_select_id=LIST_SETTING_NUM-1;
             lv_event_send(lv_obj_get_child(obj,cur_select_id), LV_EVENT_FOCUSED, NULL);
             break;
        case KEY3_CODE:
             if(++cur_select_id>=LIST_SETTING_NUM)cur_select_id=0;
             lv_event_send(lv_obj_get_child(obj,cur_select_id), LV_EVENT_FOCUSED, NULL);
             break;

        case KEY4_CODE:
             lv_event_send(lv_obj_get_child(obj,cur_select_id), LV_EVENT_CLICKED, NULL);
            break;

        }
		printf("key_code %d \n", *key);
	}

}
 
void fr_list_page(lv_obj_t* parent,lv_point_t *top)
{
	uint8_t i;
	#if 1
	char system_setting_char[LIST_SETTING_NUM][15]=
	{
		"首页",
		"仪表盘",
		"控制面板",
		"音乐",
		"CAN测试",
		"页面测试",
		"上网测试",
//		"返回",
	};
	#else
	char system_setting_char[16][15]=
	{
		"Brightness",
		"Dial plate",
		"Normally on time",
		"Coupon screen time",

		"Phone Settings",
		"Reboot",
		"Shut down",
	};
	#endif
    if(!lv_obj_is_valid(parent))
        return;

	lock_timer_flag = 1;

    UI_PARENT_INIT(parent);
	lv_obj_t *obj_cont = lv_obj_create(parent);
	lv_obj_set_size(obj_cont,LV_PCT(70),LV_PCT(92));
	lv_obj_set_style_bg_color(obj_cont,lv_color_black(),0);
	lv_obj_set_style_pad_all(obj_cont,0,0);
	lv_obj_set_style_pad_bottom(obj_cont,10,0);
	lv_obj_set_style_pad_top(obj_cont,10,0);
	lv_obj_set_style_border_width(obj_cont,0,0);
    lv_obj_set_scrollbar_mode(obj_cont, LV_SCROLLBAR_MODE_OFF);
  
	//lv_obj_set_pos(obj_cont,0,5);
	lv_obj_align(obj_cont,LV_ALIGN_CENTER,0,0);
    lv_obj_set_flex_flow(obj_cont, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(obj_cont, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
	
	lv_obj_t *old_obj;
	for(uint8_t i = 0; i < LIST_SETTING_NUM; i++)
	{
	    lv_obj_t *btn_cont = lv_obj_create(obj_cont);
	    lv_obj_set_style_bg_color(btn_cont,lv_color_black(),0);
	    lv_obj_set_style_border_color(btn_cont,lv_color_black(),0);
		lv_obj_set_size(btn_cont,LV_PCT(90),54);
		lv_obj_set_style_pad_all(btn_cont,0,0);
		lv_obj_set_style_radius(btn_cont,50,0);

		lv_obj_add_event_cb(btn_cont, system_setting_event_cb, LV_EVENT_FOCUSED, obj_cont);
		lv_obj_add_event_cb(btn_cont, system_setting_event_cb, LV_EVENT_CLICKED, obj_cont);
        lv_obj_set_scrollbar_mode(btn_cont,LV_SCROLLBAR_MODE_OFF);

		lv_obj_t* img = lv_img_create(btn_cont);
		lv_img_set_src(img,IMG_DOT_BLUE_32);
		lv_obj_align(img,LV_ALIGN_LEFT_MID,5,0);
		if(i==0)
		{
			lv_obj_align(btn_cont,LV_ALIGN_TOP_LEFT,10,5);
		}
		else
		{
			lv_obj_align_to(btn_cont,old_obj,LV_ALIGN_OUT_BOTTOM_LEFT,0,5);
		}
		lv_obj_t* dis_label = lv_label_create(btn_cont);
		lv_obj_align(dis_label,LV_ALIGN_LEFT_MID,70,0);
		//lv_obj_set_style_text_font(dis_btn_label,FONT_BIN_ARIALUNI_20,0);
        lv_obj_set_style_text_font(dis_label,LV_FONT_ARIALUNI_ALL_32PX,0);
        lv_obj_set_style_text_color(dis_label,lv_color_make(18, 150, 219),0);
		lv_label_set_text(dis_label,system_setting_char[i]);
		old_obj = btn_cont;
		lv_obj_add_flag(btn_cont, LV_OBJ_FLAG_SCROLL_ON_FOCUS);
	}
    lv_event_send(lv_obj_get_child(obj_cont,cur_select_id), LV_EVENT_FOCUSED, NULL);
    lv_obj_add_event_cb(parent, event_handler, LV_EVENT_KEY, NULL);
    //lv_group_add_obj(g_group, obj_cont);
    #if (PAGE_HCI_EN==1)
    #endif

}
